package com.atguigu.dga230301.governance.assess.spec;

import com.atguigu.dga230301.governance.assess.Assessor;
import com.atguigu.dga230301.governance.bean.AssessParam;
import com.atguigu.dga230301.governance.bean.GovernanceAssessDetail;
import com.atguigu.dga230301.meta.constant.MetaConst;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component("TABLE_NAME_STANDARD")
public class TableNameStandardAssess extends Assessor {
    Pattern odsPattern =Pattern.compile("^ods_.+_(inc|full)$");
    Pattern dwdPattern =Pattern.compile("^dwd_.+_.+_(inc|full)$");
    Pattern dimPattern =Pattern.compile("^dim_.+_(zip|full)$");
    Pattern dwsPattern =Pattern.compile("^dws_.+_.+_.+_(\\d+d|td)$");
    Pattern adsPattern =Pattern.compile("^ads_.+");
    Pattern dmPattern =Pattern.compile("^dm_.+");

    @Override
    protected void assessor(GovernanceAssessDetail governanceAssessDetail, AssessParam assessParam) throws ParseException {
        //1.获取到表名
        String tableName = assessParam.getTableMetaInfo().getTableName();

        //2.获取到层级
        String dwLevel = assessParam.getTableMetaInfo().getTableMetaInfoExtra().getDwLevel();

        //判断是否为纳入分层
        if (dwLevel.equals(MetaConst.DW_LEVEL_OTHER)){
            governanceAssessDetail.setAssessScore(BigDecimal.valueOf(5));
            governanceAssessDetail.setAssessProblem("未纳入分层");
            String url = assessParam.getGovernanceMetric().getGovernanceUrl() + assessParam.getTableMetaInfo().getId();
            governanceAssessDetail.setGovernanceUrl(url);
            return;
        }

        //3.获取对应的正则表达式
        Pattern pattern =null;
      /*  if (dwLevel.equals(MetaConst.DW_LEVEL_ODS)){
            pattern = odsPattern;
        } else if (dwLevel.equals(MetaConst.DW_LEVEL_DWD)) {
            pattern = dwdPattern;
        } else if () {

        }*/

        switch (dwLevel){
            case MetaConst.DW_LEVEL_ODS : pattern= odsPattern  ; break;
            case MetaConst.DW_LEVEL_DWD : pattern= dwdPattern  ; break;
            case MetaConst.DW_LEVEL_DWS: pattern= dwsPattern  ; break;
            case MetaConst.DW_LEVEL_DIM: pattern= dimPattern  ; break;
            case MetaConst.DW_LEVEL_DM:  pattern= dmPattern  ; break;
            case MetaConst.DW_LEVEL_ADS: pattern= adsPattern  ; break;
        }

        //4.匹配
        Matcher matcher = pattern.matcher(tableName);
        if (!matcher.matches()){
            //表名没有匹配上规则
            governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
            governanceAssessDetail.setAssessProblem("表名不符合规范");
            governanceAssessDetail.setAssessComment(pattern.toString());
        }
    }
}
